﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Bussiness;

namespace EWebsite.Admin.Partnerships {
    public partial class Default : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
            if (!IsPostBack) {
                PartnerBuss buss = new PartnerBuss();
                grvPartners.DataSource = buss.getPartnerList();
                grvPartners.DataBind();
            }
        }

        protected void grvPartners_RowEditing(object sender, GridViewEditEventArgs e) {
            grvPartners.EditIndex = e.NewEditIndex;

            PartnerBuss buss = new PartnerBuss();
            grvPartners.DataSource = buss.getPartnerList();
            grvPartners.DataBind();
        }

        protected void grvPartners_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) {
            grvPartners.EditIndex = -1;

            PartnerBuss buss = new PartnerBuss();
            grvPartners.DataSource = buss.getPartnerList();
            grvPartners.DataBind();
        }

        protected void grvPartners_RowUpdated(object sender, GridViewUpdatedEventArgs e) {

        }

        protected void grvPartners_RowUpdating(object sender, GridViewUpdateEventArgs e) {
            int index = e.RowIndex;

            int partnerID = int.Parse(((Label)grvPartners.Rows[index].FindControl("lblPartnerID")).Text);
            String name = ((TextBox)grvPartners.Rows[index].FindControl("txtName")).Text;
            String wsName = ((TextBox)grvPartners.Rows[index].FindControl("txtWSName")).Text;
            String wsLink = ((TextBox)grvPartners.Rows[index].FindControl("txtLinkWS")).Text;
            String pvdID = ((TextBox)grvPartners.Rows[index].FindControl("txtPvdID")).Text;
            String pvdPwd = ((TextBox)grvPartners.Rows[index].FindControl("txtPvdPwd")).Text;
            String partnerLogin = ((TextBox)grvPartners.Rows[index].FindControl("txtPartnerLogin")).Text;            
            String partnerPwd = ((TextBox)grvPartners.Rows[index].FindControl("txtPartnerPwd")).Text;
            
            if(partnerPwd.Trim().Equals("") || pvdPwd.Trim().Equals("")){ //password not empty
                Response.Write("<script>alert('mat khau khong duoc de trong');</scipt>");
            }else{                
                //save to db
                PartnerBuss buss = new PartnerBuss();
                buss.update(partnerID, name, partnerLogin, partnerPwd, wsName, wsLink, pvdID, pvdPwd); 
            }

            PartnerBuss buss2 = new PartnerBuss();
            grvPartners.DataSource = buss2.getPartnerList();
            grvPartners.EditIndex = -1;
            grvPartners.DataBind();
        }

        protected void btnInsert_Click(object sender, EventArgs e) {
            String name = tName.Text;
            String wsName = tWSName.Text;
            String wsLink = tWSLink.Text;
            String pvdID = tPvdID.Text;
            String pvdPwd = tPvdPwd.Text;
            String partnerLogin = tLogin.Text;
            String partnerPwd = tPwd.Text;

            if (name.Equals("") || wsName.Equals("") || wsLink.Equals("") || pvdID.Equals("") || pvdPwd.Equals("") || partnerLogin.Equals("") || partnerPwd.Equals("")) {
                Response.Write("<script>alert('khong duoc de trong');</scipt>");
            }
            else { 
                //save to db
                PartnerBuss buss = new PartnerBuss();
                buss.insert(name, partnerLogin, partnerPwd, wsName, wsLink, pvdID, pvdPwd); 
            }

            PartnerBuss buss2 = new PartnerBuss();
            grvPartners.DataSource = buss2.getPartnerList();
            grvPartners.EditIndex = -1;
            grvPartners.DataBind();
        }
    }
}